$(function() {
    FastClick.attach(document.body);
    $(document).on('click', '.pop-shadow', function (event) {
        $('.pop').fadeOut();
        $('.pop').remove();
    });
});

var shaohuo = window.shaohuo || {};
shaohuo.javascriptBridge = new function() {
    setupWebViewJavascriptBridge = function(callback) {
        if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
        if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
        window.WVJBCallbacks = [callback];
        var WVJBIframe = document.createElement('iframe');
        WVJBIframe.style.display = 'none';
        WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
        document.documentElement.appendChild(WVJBIframe);
        setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
    };
    
    this.onPageAction = function(params) {
        self.setupWebViewJavascriptBridge(function(bridge){
          bridge.callHandler("onPageAction",params, function(response){
                             params.success(response)
                });
          });
    };
};


//跳转至我的申请资料去
function jump2UploadInfo(apply_status) {
    if (!is_shaohuo()) {
        return;
    }
    var target = "UploadInfo";
    if (apply_status == 2) {
        target = "ViewUploadInfo";
    } else if (apply_status == 3){
        target = "UpdateUploadInfo";
    }

    shaohuo.javascriptBridge.onPageAction({
      "title": "title",
      "action": "push",
      "target": target,
      "params": "",
      "scame": ""
    });
}
//跳转至接单列表
function jump2ShippingList() {
    if (!is_shaohuo()) {
        return;
    }
    shaohuo.javascriptBridge.onPageAction({
      "title": "title",
      "action": "push",
      "target": "ShippingList",
      "params": "",
      "scame": ""
    });
}
//判断是否是捎货app中打开，
function is_shaohuo() {
	var angentArr = returnUserAngentArray();
	if (angentArr.is_ios != 1 && angentArr.is_android != 1) {
		location.href = 'http://www.ishaohuo.cn/download/';
		return false;
	}
	return true;
}
//返回设备类型数组
function returnUserAngentArray(){
    var returnAry = {'is_weixin':0,'is_ios':0,'is_android':0};
    var ua = window.navigator.userAgent.toLowerCase();
    // var angent = 'Mozilla/5.0 (Linux; Android 5.1.1; SM-N910S Build/LMY47X; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36 ishaohuo/1.9';
    // var ua = angent.toLowerCase();
    //判断微信
    if(ua.match(/MicroMessenger/i) == 'micromessenger'){
        returnAry.is_weixin = 1;
    }
    //判断ios
    if(ua.match(/iPhone/i) == 'iphone' && ua.match(/ishaohuo/i) == 'ishaohuo'){
        returnAry.is_ios = 1;
    }
    //判断android
    if(ua.match(/Android/i) == 'android' && ua.match(/ishaohuo/i) == 'ishaohuo'){
        returnAry.is_android = 1;
    }
    return returnAry;
}

//进入下一节
function nextChapter(data, params){
    var queryParams = [];
    for(var i in params) {
        queryParams.push(i + '=' + params[i]);
    }
    location.href="/api/exam/chapter" + data.next + '?' + queryParams.join('&');
}

//返回主页
function backMain(data, params) {
    var queryParams = [];
    for(var i in params) {
        queryParams.push(i + '=' + params[i]);
    }
    location.href="/api/exam/index?" + queryParams.join('&');
}

//记录已阅读
function readChapter(chapter, callback) {
    chapter = parseInt(chapter);
    if (chapter <= 0) {return;}
    
    $.post("/api/exam/updateExamStatus", {chapter: chapter, uid: uid},
        function(result){
            if (result.errcode == 0) {
                if (callback != null && callback != undefined) {
                    callback(result.data, {uid: uid});
                }
            } else {
                alert(result.data.errmsg);
            }
        },'json');
}

//调用提示信息
function tipsDialog(msg) {
    var tpl = '<div class="pop">'
        +'<div class="pop-shadow"></div>'
        +'<div class="pop-content">'
            +'<div class="title text-center">'
                +'<span>!</span>'
                +'<h6>捎货提醒你</h6>'
            +'</div>'
            +'<div class="msg">'
            +'_MSG_</div>'
        +'</div>'
    +'</div>';
    $(".pop .pop-content .msg").text(msg);
    $('body').append(tpl.replace('_MSG_', msg));
    $(".pop").fadeIn();
}

//统一页面跳转
function jump2app(target){
    if (!is_shaohuo()) {
        return;
    }
    shaohuo.javascriptBridge.onPageAction({
      "title": "title",
      "action": "push",
      "target": target,
      "params": "",
      "scame": ""
    });
}